package cn.stylefeng.guns.modular.school.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.util.DictTypeConstant;
import cn.stylefeng.guns.modular.school.entity.StudentInfo;
import cn.stylefeng.guns.modular.school.model.params.StudentInfoParam;
import cn.stylefeng.guns.modular.school.model.result.ClassInfoResult;
import cn.stylefeng.guns.modular.school.model.result.SchoolInfoResult;
import cn.stylefeng.guns.modular.school.model.result.TeacherInfoResult;
import cn.stylefeng.guns.modular.school.service.ClassInfoService;
import cn.stylefeng.guns.modular.school.service.SchoolInfoService;
import cn.stylefeng.guns.modular.school.service.StudentInfoService;
import cn.stylefeng.guns.modular.school.service.TeacherInfoService;
import cn.stylefeng.guns.sys.modular.system.entity.Dict;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 学生信息控制器
 *
 * @author
 * @Date 2019-11-15 09:51:41
 */
@Controller
@RequestMapping("/studentInfo")
public class StudentInfoController extends BaseController {

    private String PREFIX = "/school/studentInfo";

    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private SchoolInfoService schoolInfoService;
    @Autowired
    private DictService dictService;
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private TeacherInfoService teacherInfoService;

    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2019-11-15
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/studentInfo.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-11-15
     */
    @RequestMapping("/add")
    public String add() {
        init();
        return PREFIX + "/studentInfo_add.html";
    }

    private void init() {
        // 教师
        List<TeacherInfoResult> teachers = teacherInfoService.findListBySpec(null);
        setAttr("teachers", teachers);
        // 学校
        List<SchoolInfoResult> schools = schoolInfoService.findListBySpec(null);
        setAttr("schools", schools);
        // 性别
        List<Dict> SEX = dictService.listDictsByCode(DictTypeConstant.SEX);
        setAttr("SEX", SEX);
        // 监护人关系
        List<Dict> JHRGX = dictService.listDictsByCode(DictTypeConstant.JHRGX);
        setAttr("JHRGX", JHRGX);
        // 班级
        List<ClassInfoResult> classes = classInfoService.findListBySpec(null);
        classes.forEach(clazz -> {
            clazz.setName("代码：" + clazz.getCode() + " 名称：" + clazz.getName() + " 班主任：" + clazz.getTeacherName());
        });
        setAttr("classes", classes);
        // 学生在读状态
        List<Dict> XSZDZT = dictService.listDictsByCode(DictTypeConstant.XSZDZT);
        setAttr("XSZDZT", XSZDZT);
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-11-15
     */
    @RequestMapping("/edit")
    public String edit() {
        init();
        return PREFIX + "/studentInfo_edit.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-11-15
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(StudentInfoParam studentInfoParam) {
        this.studentInfoService.add(studentInfoParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2019-11-15
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(StudentInfoParam studentInfoParam) {
        this.studentInfoService.update(studentInfoParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2019-11-15
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(StudentInfoParam studentInfoParam) {
        this.studentInfoService.delete(studentInfoParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2019-11-15
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(StudentInfoParam studentInfoParam) {
        StudentInfo detail = this.studentInfoService.getById(studentInfoParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-11-15
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(StudentInfoParam studentInfoParam) {
        return this.studentInfoService.findPageBySpec(studentInfoParam);
    }

}


